package com.zhxh.nzpos.view;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.widget.EditText;

/**
 * 用于弹出窗口修改EditText中日期
 * @author wangzt
 *
 */
public class CustomDatePicker {

	private DatePickerDialog picker;
	
	private Calendar cal; 
	public CustomDatePicker(Activity activity, EditText text, Calendar cal) {
		this.cal = cal;
		picker = new DatePickerDialog(
				activity,
				new DateSetListener(text, cal),
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH));
	}

	public void show() {
		if (picker != null) {
			picker.show();
		}
	}
	
	public void updateDate() {
		if (picker != null && cal != null) {
			picker.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DAY_OF_MONTH));
		}
	}
	
	private class DateSetListener implements DatePickerDialog.OnDateSetListener {

		private EditText text;
		private Calendar cal;
		
		public DateSetListener(EditText text, Calendar cal) {
			this.text = text;
			this.cal = cal;
		}
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        text.setText(simpleDateFormat.format(cal.getTime()));
		}
		
	}
}
